﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Antu.DataAccess;
using Antu.SystemLayer;

namespace Antu.Business
{
    public class SetTab
    {
        #region 得到清单
    
        /// <summary>
        /// 根据传入条件取得列表
        /// </summary>
        /// <param name="id">为空表示全部</param>
        /// <param name="workflow">流程名称，为空表示全部</param>
        /// <param name="tableName">表格名称，为空表示全部</param>
        /// <param name="fieldName">字段名称，为空表示全部</param>
        /// <param name="chuFaType">触发类型，为空表示全部</param>
        /// <param name="chuFaNode">触发节点，为空表示全部</param>
        /// <param name="type">类型</param>        
        /// <returns></returns>
        public DataTable GetList(string id,string workflow,string tableName,string fieldName,string chuFaType,string chuFaNode,string type) 
        {
            IDataAccess ida = SysParams.OAConnection();
            string sql = "select * from xt_taskfieldsync WHERE 1=1 ";
            if (!String.IsNullOrEmpty(id))
                sql += " AND ID=" + id;
            if (!String.IsNullOrEmpty(workflow))
                sql += " AND WorkFlowName='" + workflow + "'";
            if (!String.IsNullOrEmpty(tableName))
                sql += " AND TableName='"+tableName+"'";
            if (!String.IsNullOrEmpty(fieldName))
                sql += " AND fileName='" + fieldName + "'";
            if (!String.IsNullOrEmpty(chuFaType))
                sql += " AND FireType=" + chuFaType;
            if (!String.IsNullOrEmpty(chuFaNode))
                sql += " AND FireStep=" + chuFaNode;
            if (!String.IsNullOrEmpty(type))
                sql += " AND FireTime=" + type;
            DataTable dt;
            ida.RunSql(sql, out dt);
            ida.Close();
            return dt;
        }
        #endregion
    
        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">序号</param>
       
        public void Delete(string id) 
        {
            IDataAccess ida = SysParams.OAConnection();
            string sql = "delete from xt_taskfieldsync where id='" + id + "'";
            ida.RunSql(sql);
            ida.Close();
        }
        #endregion


        #region 保存
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="ID">序号</param>
        /// <param name="tabNameNew">表格名称</param>
        /// <param name="fieldNameNew">字段名</param>
        /// <param name="fieldDiscriptionNew">字段描述</param>
        /// <param name="processName">流程名称</param>
        /// <param name="type">类型</param>
        /// <param name="chufaType">触发类型</param>
        /// <param name="chufaNode">触发节点</param>
        public string Update(string id,string tabName,string fieldName,string fieldDiscription,string processName,
                            string type,string chufaType,string chufaNode) 
        {
            IDataAccess ida = SysParams.OAConnection();
           // DataTable dt;
            string sql = "",result="";
            if (String.IsNullOrEmpty( id)) //新增
            {
                long number = SystemTable.GetMax("xt_taskfieldsync");
                sql = "insert into xt_taskfieldsync(TableName,FieldName,FieldValue,id,WorkFlowName,FireType,FireTime,FireStep) values ('" 
                    + tabName + "','" + fieldName + "','" + fieldDiscription + "','" + number.ToString() + "','"
                    +processName+"','"+type+"','"+chufaType+"','"+chufaNode+"')";              
            }
            else //修改
            {
                sql = "update xt_taskfieldsync set TableName='" + tabName + "' , FieldName='" + fieldName 
                    + "' , FieldValue='" + fieldDiscription + "',WorkFlowName='"+processName+"',FireType='"+type+"',FireTime='"
                    +chufaType+"',FireStep='"+chufaNode+"' where ID='"+id+"'";
            }
            try
            {
                ida.RunSql(sql);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            finally
            {
                ida.Close();              
            }            
            return result; 
        }
        #endregion

              
        #region 验证格式是否正确
        /// <summary>
        /// 验证格式是否正确
        /// </summary>
        /// <param name="description">描述</param>
        /// <param name="serial">流程号</param>
        public bool YanZheng(string description) 
        {
            IDataAccess ida = SysParams.OAConnection();
            DataTable dt;
            string sql = "select serial from st_workflow";
            ida.RunSql(sql, out dt);
            ida.Close();
            Int64 serialNumber = Int64.Parse(dt.Rows[0][0].ToString());
            IDataParameter[] idParms = DataFactory.GetParameter(DatabaseType.Oracle, 2);
            idParms[0].ParameterName = "iDescription";
            idParms[0].Value = description;

            idParms[1].ParameterName = "iSerial";
            idParms[1].Value = serialNumber;

            try
            {
                ida.RunProc("Base.To_String", ref idParms);
                return true;
            }
            catch 
            {
                return false;
            }
        }
        #endregion
    }
}
